/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.common;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.commonmark.Extension;
import org.commonmark.ext.gfm.tables.TableBlock;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
import org.commonmark.node.Link;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.AttributeProvider;
import org.commonmark.renderer.html.AttributeProviderContext;
import org.commonmark.renderer.html.AttributeProviderFactory;
import org.commonmark.renderer.html.HtmlRenderer;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/9/25 11:13
 * md格式转为html格式
 */
public class MDTOHtml {
    private String textMd;

    private static volatile MDTOHtml instance;

    MDTOHtml(String contentText)
    {
        this.textMd = contentText;
    }

    private void setTextMd(String contentText){
        this.textMd = contentText;
    }

    // 创建类

    public static synchronized MDTOHtml getInstance(String contentText) {
        if (instance == null) {
            synchronized (DetermineLogin.class) {
                if (instance == null) {
                    instance = new MDTOHtml(contentText);
                }
            }
        }
        instance.setTextMd(contentText);
        return instance;
    }

//    public
    public String toHtml() {
        Parser parser = Parser.builder().build();
        Node document = parser.parse(this.textMd);
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        return renderer.render(document);
    }

    /**
     * 增加扩展[标题锚点，表格生成]
     * Markdown转换成HTML
     * @return
     */
    public String toHtmlExtensions() {
        //h标题生成id
        Set<Extension> headingAnchorExtensions = Collections.singleton(HeadingAnchorExtension.create());
        //转换table的HTML
//        List<Extension> tableExtension = Arrays.asList(TablesExtension.create());
        List<Extension> tableExtension = Collections.singletonList(TablesExtension.create());
        Parser parser = Parser.builder()
                .extensions(tableExtension)
                .build();
        Node document = parser.parse(this.textMd);
        HtmlRenderer renderer = HtmlRenderer.builder()
                .extensions(headingAnchorExtensions)
                .extensions(tableExtension)
                .attributeProviderFactory(new AttributeProviderFactory() {
                    public AttributeProvider create(AttributeProviderContext context) {
                        return new CustomAttributeProvider();
                    }
                })
                .build();
        return renderer.render(document);
    }

    /**
     * 处理标签的属性
     */
    static class CustomAttributeProvider implements AttributeProvider {
        @Override
        public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
            //改变a标签的target属性为_blank
            if (node instanceof Link) {
                attributes.put("target", "_blank");
            }
            if (node instanceof TableBlock) {
                attributes.put("class", "ui celled table");
            }
        }
    }
}


